package com.escom.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;

import com.escom.dao.MedicoDAO;
import com.escom.entidad.MedicoVO;
import com.escom.utils.Constantes;

import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Carlos on 14/04/2014.
 */
public class MedicoWebService implements Runnable {

    private Handler handler;
    private Context context;
    private String nick, pass;
    private SharedPreferences sharedPreferences;

    @Override
    public void run() {
        Log.d("Logger ESCOM: ", "run");

        String url = "http://" + Constantes.IP_PC + ":8080/TrabajoTerminal-web/rest/medicoWebService?" +
                "nick={query}&pass={query}";
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

        ClientHttpRequestFactory clientHttpRequestFactory = restTemplate.getRequestFactory();
        int timeout = 10000;
        if (clientHttpRequestFactory instanceof SimpleClientHttpRequestFactory) {
            ((SimpleClientHttpRequestFactory) clientHttpRequestFactory).setConnectTimeout(timeout);
            ((SimpleClientHttpRequestFactory) clientHttpRequestFactory).setReadTimeout(timeout);
        } else if (clientHttpRequestFactory instanceof HttpComponentsClientHttpRequestFactory) {
            ((HttpComponentsClientHttpRequestFactory) clientHttpRequestFactory).setReadTimeout(timeout);
            ((HttpComponentsClientHttpRequestFactory) clientHttpRequestFactory).setConnectTimeout(timeout);
        }

        MedicoVO resultMedico = null;
        try {
            resultMedico = restTemplate.getForObject(url, MedicoVO.class, nick, pass );
        } catch (ResourceAccessException exResource) {
            Log.e("Logger Error: {}", exResource.fillInStackTrace().toString());
        }

        if (resultMedico != null){
            MedicoDAO medicoDAO = new MedicoDAO(context);
            medicoDAO.createMedico(resultMedico);
            medicoDAO.setHandler(handler);
            medicoDAO.setSharedpreferences(sharedPreferences);
            medicoDAO.existMedicoByNickPass(nick, pass);
        }
    }


    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public void setSharedPreferences(SharedPreferences sharedPreferences) {
        this.sharedPreferences = sharedPreferences;
    }
}
